var index = parent.layer.getFrameIndex(window.name);
function registerAuth(k,obj) {
	var obj = $(obj);
	var parentDom = $("#tr"+k);
	if(!obj.hasClass("isclick")) {
		obj.attr("disabled", true);
		obj.addClass("isclick");
		main  = parentDom.find(".main").val();
		title = parentDom.find(".title").val();
		if(main == "") {
			layer.msg("数据错误，权限主体不存在！", {icon: 2,shadeClose: true,shade:[0.5 , 'pink' , true]},function(){
				obj.removeAttr("disabled");
				obj.removeClass("isclick");
			});
		}
		obj.removeClass("isclick");
		title = title == ""?main:title;
		var uri = page_url;
		ajaxPost(uri,obj,{'main':main,'title':title},function(){
			parentDom.find("#tr"+k+"_status").find("font").attr("color","green");
			parentDom.find("#tr"+k+"_status").find("font").html("已注册");
		});
	}
}

function saveClass(k,obj) {
	var obj = $(obj);
	var parentDom = $("#tr"+k);
	if(!obj.hasClass("isclick")) {
		obj.attr("disabled", true);
		obj.addClass("isclick");
		name  = parentDom.find(".name").val();
		tag   = parentDom.find(".tag").val();
		if(name == "") {
			layer.msg("请填写分类名字", {icon: 2,shadeClose: true,shade:[0.5 , 'pink' , true]},function(){
				obj.removeAttr("disabled");
				obj.removeClass("isclick");
			});
		}
		if(tag == "") {
			layer.msg("请填写分类标识", {icon: 2,shadeClose: true,shade:[0.5 , 'pink' , true]},function(){
				obj.removeAttr("disabled");
				obj.removeClass("isclick");
			});
		}
		obj.removeClass("isclick");
		name = name == ""?tag:name;
		var uri = $("form[name=agent-class]").attr("action")+"?r="+Math.random();
		ajaxPost(uri,obj,{'name':name,'tag':tag});
	}
}

$("#auth-register").on("click", function(){
	var obj = $(this);
	var domParent = parent;
	obj.attr("disabled", true);

	var uri = page_url;
	var data = $("form[name=auth-register]").serialize();
	
	ajaxPost(uri,obj,data,function(){
		location.reload();
	});
});
$("#agent-add").click(function(){
	$("#agent-add").attr("disabled", true);
	var domParent = parent;
	name      = $("#ag_name").val();
	title     = $("#ag_title").val();
	type      = $("#ag_type").val();
	status    = $("#ag_status").val();
	condition = $("#ag_condition").val();
	
	if(name == "") {
		layer.tips('请填写权限主体！', $("#ag_name"), {
			tips: [1, '#d9534f'],
			time: 4000
		});
		$("#agent-add").removeAttr("disabled");
		return;
	}
	if(title == "") {
		layer.tips('请填写权限名字！', $("#ag_title"), {
			tips: [1, '#d9534f'],
			time: 4000
		});
		$("#agent-add").removeAttr("disabled");
		return;
	}
	if(type == "") {
		layer.tips('请选择权限类型！', $("#ag_type"), {
			tips: [1, '#d9534f'],
			time: 4000
		});
		$("#agent-add").removeAttr("disabled");
		return;
	}
	if(status == "") {
		layer.tips('请选择权限状态！', $("#ag_status"), {
			tips: [1, '#d9534f'],
			time: 4000
		});
		$("#agent-add").removeAttr("disabled");
		return;
	}
	var uri  = $("form[name=agent-add]").attr("action")+"?r="+Math.random();
	var data = $("form[name=agent-add]").serialize();
	ajaxPost(uri, $("#agent-add"), data, function(){
		domParent.$(".link-popup").removeAttr("disabled");
		domParent.location.reload();
		domParent.layer.close(index);
	});
});

$("#agent-save").click(function(){
	$("#agent-save").attr("disabled", true);
	var domParent = parent;
	name      = $("#ag_name").val();
	title     = $("#ag_title").val();
	type      = $("#ag_type").val();
	status    = $("#ag_status").val();
	condition = $("#ag_condition").val();
	
	if(name == "") {
		layer.tips('请填写权限主体！', $("#ag_name"), {
			tips: [1, '#d9534f'],
			time: 4000
		});
		$("#agent-save").removeAttr("disabled");
		return;
	}
	if(title == "") {
		layer.tips('请填写权限名字！', $("#ag_title"), {
			tips: [1, '#d9534f'],
			time: 4000
		});
		$("#agent-save").removeAttr("disabled");
		return;
	}
	if(type == "") {
		layer.tips('请选择权限类型！', $("#ag_type"), {
			tips: [1, '#d9534f'],
			time: 4000
		});
		$("#agent-save").removeAttr("disabled");
		return;
	}
	if(status == "") {
		layer.tips('请选择权限状态！', $("#ag_status"), {
			tips: [1, '#d9534f'],
			time: 4000
		});
		$("#agent-save").removeAttr("disabled");
		return;
	}
	var uri  = $("form[name=agent]").attr("action")+"?r="+Math.random();
	var data = $("form[name=agent]").serialize();
	ajaxPost(uri, $("#agent-save"), data, function(){
		domParent.$(".link-popup").removeAttr("disabled");
		domParent.location.reload();
		domParent.layer.close(index);
	});
});

$("#agent-class").on("click", function(){
	var obj = $(this);
	var domParent = parent;
	obj.attr("disabled", true);

	var uri = $("form[name=agent-class]").attr("action")+"?r="+Math.random();
	var data = $("form[name=agent-class]").serialize();
	
	ajaxPost(uri,obj,data,function(){
		location.reload();
	});
});

$("#agent_add").on("click", function() {
	container = $("#agent_table tbody");
	tag = parseInt(container.find("tr:last-child").attr("id").replace("tr",""))+1;
	content = "";
	content += '<tr id="tr'+tag+'">\r\n';
	content += '    <td>'+tag+'</td>\r\n';
	content += '    <td>\r\n';
	content += '    	<span id="lb_name_'+tag+'"></span>\r\n';
	content += '    	<input id="in_name_'+tag+'" type="hidden" class="name" name="name[]" value="">\r\n';
	content += '    	<a href="javascript:void(0)" class="link-prompt" data-prompt="" data-to="#in_name_'+tag+'" data-show="#lb_name_'+tag+'">\r\n';
	content += '    		<i class="fa fa-edit"></i>\r\n';
	content += '    	</a>\r\n';
	content += '    </td>\r\n';
	content += '    <td>\r\n';
	content += '    	<span id="lb_tag_'+tag+'"></span>\r\n';
	content += '    	<input id="in_tag_'+tag+'" type="hidden" class="tag" name="tag[]" value="">\r\n';
	content += '    	<a href="javascript:void(0)" class="link-prompt" data-prompt="" data-to="#in_tag_'+tag+'" data-show="#lb_tag_'+tag+'">\r\n';
	content += '    		<i class="fa fa-edit"></i>\r\n';
	content += '    	</a>\r\n';
	content += '    </td>\r\n';
	content += '    <td>\r\n';
	content += '    	<a href="javascript:void(0);" onclick="saveClass('+tag+',this)">保存</a>\r\n';
	content += '    </td>\r\n';
	content += '</tr>\r\n';
	$(container).append(content);
});